package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.RechargeViewHolder;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.UserRechargeResponse;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class PurchaseMonthlyFragment extends Fragment {
    private String value;
    private String key;
    private View view;
    private RecyclerView rv_space;
    private PurchaseMonthlyAdapter adapter;


    private List<String> mList = new ArrayList<>();

    public PurchaseMonthlyFragment(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_purchaseinfo, container, false);
            setViewId();
            setListener();
        }
        return view;
    }

    public void setViewId() {
        rv_space = view.findViewById(R.id.rv_rechargeinfo);
        rv_space.setNestedScrollingEnabled(false);
        rv_space.setFocusable(false);
        rv_space.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_space.addItemDecoration(new LinearLayouDecoration(getActivity(),
                LinearLayouDecoration.HORIZONTAL, R.dimen.border, R.color.transparent));
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setListener() {

        rv_space.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new PurchaseMonthlyAdapter(getActivity());
        rv_space.setAdapter(adapter);


        afterCreate();
    }


    public void afterCreate() {
        ((FFActivity) getActivity()).post(Url.listUserMonthSubscribe, null, UserRechargeResponse.class, new FFNetWorkCallBack<UserRechargeResponse>() {
            @Override
            public void onSuccess(UserRechargeResponse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }

            }

            @Override
            public boolean onFail(UserRechargeResponse response) {
                return false;
            }
        },"{}");


    }


    public class PurchaseMonthlyAdapter extends BaseRecyclerAdapter<RechargeViewHolder> {
        private Context context;


        private List<UserRechargeResponse.DataBean> mData = new ArrayList<>();


        public PurchaseMonthlyAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<UserRechargeResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<UserRechargeResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public RechargeViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.recharge_item, parent, false);
            RechargeViewHolder viewHolder_text = new RechargeViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public RechargeViewHolder getViewHolder(View view) {
            return new RechargeViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final RechargeViewHolder holder, final int position, boolean isItem) {
            holder.tv_num.setText("￥ " + StUtils.percentage(mData.get(position).getPaymentAmount()));
            if (mData.get(position).getMonthSubscribeType().equals("COMMON_MONTH_SUBSCRIBE")) {
                holder.tv_price.setText("包月");
            } else {
                holder.tv_price.setText("连续包月");
            }
            holder.tv_time.setText(FFUtils.stringToDates(mData.get(position).getCreateTime() + ""));


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }

}